﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Eniux.Windows.Dock
{
    /// <summary>
    /// 内容管理集合类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ManagedContentCollection<T> : ReadOnlyObservableCollection<T> where T : ManagedContent
    {

        #region 构造

        internal ManagedContentCollection(DockingManager manager)
            : base(new ObservableCollection<T>())
        {
            Manager = manager;
        } 

        #endregion

        #region 属性

        /// <summary>
        /// Dock管理器实例
        /// </summary>
        public DockingManager Manager { get; private set; }
        
        #endregion

        #region 重写

        /// <summary>
        /// 集合改变事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (T cntAdded in e.NewItems)
                    cntAdded.Manager = Manager;
            }

            base.OnCollectionChanged(e);
        } 

        #endregion

        #region 方法

        internal void Add(T contentToAdd)
        {
            if (!Items.Contains(contentToAdd))
                Items.Add(contentToAdd);
        }

        internal void Remove(T contentToRemove)
        {
            Items.Remove(contentToRemove);
        } 
        #endregion
    }
}
